package com.example.lazyhelper.ui.offer;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lazyhelper.*;

import java.util.List;

public class OfferListAdapter extends RecyclerView.Adapter<OfferListAdapter.ViewHolder> {

    private List<DataItem> mItems;
    private Context mContext;

    // 保存列表项布局的容器
    static class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        TextView itemText;
        TextView itemText2;
        public ViewHolder(View view) {
            super(view);
            itemView = view;
            itemText = view.findViewById(R.id.offer_item_text);
            itemText2 = view.findViewById(R.id.offer_item_text2);
        }
    }

    public OfferListAdapter(Context context, List<DataItem> items) {
        this.mContext = context;
        this.mItems = items;
    }

    @NonNull
    @Override
    public OfferListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 为列表项加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.offer_item, parent, false);
        return new OfferListAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OfferListAdapter.ViewHolder holder, final int position) {
        DataItem item = mItems.get(position);               // 获取列表项
        holder.itemText.setText(item.text);                 // 为布局保持器设置列表项中的内容
        holder.itemText2.setText(item.text2);
        holder.itemView.setOnClickListener(new View.OnClickListener() {     // 设置点击事件
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, OfferActivity.class);  // 设置活动意图
                intent.putExtra("request", mItems.get(position).toStrings());   // 附加参数
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

}
